package com.tebiecloud.goods.server.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tebiecloud.common.mybatis.query.CriteriaQuery;
import com.tebiecloud.common.utils.StringUtils;
import com.tebiecloud.goods.client.model.entity.Goods;
import com.tebiecloud.goods.client.model.entity.GoodsSpecification;
import com.tebiecloud.goods.client.model.vo.GoodsSpecificationVO;
import com.tebiecloud.goods.server.service.GoodsService;
import com.tebiecloud.goods.server.service.GoodsSpecificationService;
import com.tebiecloud.common.model.PageParams;
import com.tebiecloud.common.model.ResultBody;
import com.tebiecloud.common.utils.ResultBodyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.Map;

/**
 * 商品的规格信息 前端控制器
 * @author tebie
 * @date 2019-09-15
 */
@RestController
@RequestMapping("/goodsspecification")
public class GoodsSpecificationController {

    @Autowired
    private GoodsSpecificationService goodsSpecificationService;

    @Autowired
    private GoodsService goodsService;

    /**
     * 获取分页数据
     * @param page
     * @param limit
     * @param storeId
     * @return
     */
    @PostMapping(value = "list")
    public ResultBody list(
            @RequestParam(value = "page", required = false, defaultValue = "1") int page,
            @RequestParam(value = "limit", required = false, defaultValue = "10") int limit,
            @RequestParam("storeId") long storeId) {
        // 查询条件
        CriteriaQuery criteriaQuery = new CriteriaQuery(new PageParams(page, limit));
        criteriaQuery.select("specification.*", "goods.store_id storeId", "goods.name goodsName", "goods.share_pic goodsPic");
        criteriaQuery.eq("goods.store_id", storeId);
        criteriaQuery.eq("specification.deleted",0);
        criteriaQuery.orderByAsc("goods.sort", "goods.ct");
        IPage<GoodsSpecificationVO> pageGoodsSpecificationVO = goodsSpecificationService.pageList(criteriaQuery);

        return ResultBodyUtils.ok(pageGoodsSpecificationVO);
    }


    /**
     * 删除规格
     * @param specId 单个id
     * @return
     */
    @PostMapping("remove")
    public ResultBody remove(@RequestParam(value = "specId", required = false) long specId) {
        GoodsSpecification goodsSpecification = new GoodsSpecification();
        goodsSpecification.setSpecId(specId);
        goodsSpecification.setDeleted(1);
        goodsSpecificationService.updateById(goodsSpecification);
        return ResultBodyUtils.ok();
    }



//    /**
//     * 根据ID查找数据
//     * @param id
//     * @return
//     */
//    @GetMapping("get")
//    public ResultBody<GoodsSpecification> get(@RequestParam("id") Long id) {
//        GoodsSpecification entity = goodsSpecificationService.getById(id);
//        return ResultBodyUtils.ok(entity);
//    }
//
//    /**
//     * 添加数据
//     * @param goodsId 商品id,
//     * @param name 规格名称,
//     * @param price 销售价(单位：分),
//     * @param retailPrice 门市价(单位：分),
//     * @param costPrice 成本价(单位：分),
//     * @param stockQty 库存数量：-99表示不限制库存,
//     * @param down 上下架状态：0上架，1下架,
//     * @param salesVolume 销量,
//     * @param deleted 删除状态：0没有删除1已被删除
//     * @return
//     */
//    @PostMapping("add")
//    public ResultBody add(
//        @RequestParam(value = "goodsId", required = false) Long goodsId,
//        @RequestParam(value = "name", required = false) String name,
//        @RequestParam(value = "price", required = false) Integer price,
//        @RequestParam(value = "retailPrice", required = false) Integer retailPrice,
//        @RequestParam(value = "costPrice", required = false) Integer costPrice,
//        @RequestParam(value = "stockQty", required = false) Integer stockQty,
//        @RequestParam(value = "down", required = false) Integer down,
//        @RequestParam(value = "salesVolume", required = false) Integer salesVolume,
//        @RequestParam(value = "deleted", required = false) Integer deleted
//    ) {
//        GoodsSpecification entity = new GoodsSpecification();
//        entity.setGoodsId(goodsId);
//        entity.setName(name);
//        entity.setPrice(price);
//        entity.setRetailPrice(retailPrice);
//        entity.setCostPrice(costPrice);
//        entity.setStockQty(stockQty);
//        entity.setDown(down);
//        entity.setSalesVolume(salesVolume);
//        entity.setDeleted(deleted);
//        goodsSpecificationService.save(entity);
//        return ResultBodyUtils.ok();
//    }
//
//    /**
//     * 更新数据
//     * @param goodsId 商品id
//     * @param name 规格名称
//     * @param price 销售价(单位：分)
//     * @param retailPrice 门市价(单位：分)
//     * @param costPrice 成本价(单位：分)
//     * @param stockQty 库存数量：-99表示不限制库存
//     * @param down 上下架状态：0上架，1下架
//     * @param salesVolume 销量
//     * @param deleted 删除状态：0没有删除1已被删除
//     * @return
//     */
//    @PostMapping("update")
//    public ResultBody update(
//            @RequestParam(value = "specId", required = false) Long specId,
//            @RequestParam(value = "goodsId", required = false) Long goodsId,
//            @RequestParam(value = "name", required = false) String name,
//            @RequestParam(value = "price", required = false) Integer price,
//            @RequestParam(value = "retailPrice", required = false) Integer retailPrice,
//            @RequestParam(value = "costPrice", required = false) Integer costPrice,
//            @RequestParam(value = "stockQty", required = false) Integer stockQty,
//            @RequestParam(value = "down", required = false) Integer down,
//            @RequestParam(value = "salesVolume", required = false) Integer salesVolume,
//            @RequestParam(value = "deleted", required = false) Integer deleted
//    ) {
//        GoodsSpecification entity = new GoodsSpecification();
//        entity.setSpecId(specId);
//        entity.setGoodsId(goodsId);
//        entity.setName(name);
//        entity.setPrice(price);
//        entity.setRetailPrice(retailPrice);
//        entity.setCostPrice(costPrice);
//        entity.setStockQty(stockQty);
//        entity.setDown(down);
//        entity.setSalesVolume(salesVolume);
//        entity.setDeleted(deleted);
//        goodsSpecificationService.updateById(entity);
//        return ResultBodyUtils.ok();
//    }
//


}
